import base64
from io import BytesIO

from PIL import Image

# copy from dots.ocr image_utils.py PILimage_to_base64
def page_image_to_base64(image: Image, file_format='png'):
    buffer = BytesIO()
    image.save(buffer, format=file_format)
    base64_str = base64.b64encode(buffer.getvalue()).decode('utf-8')
    return f"data:image/{file_format.lower()};base64,{base64_str}"

def image_cut(img_path, start: tuple[int, int], end: tuple[int, int]) -> Image:
    """
    按照指定的坐标裁剪图片
    :param img_path: 图片文件路径
    :param start: 裁剪区域左上角坐标点
    :param end: 裁剪区域右下角坐标点
    :return:
    """
    image = Image.open(img_path)
    cut_tuple = (start[0], start[1], end[0], end[1])
    return image.crop(cut_tuple)

def get_image_size(img_path) -> tuple[int, int]:
    """
    获取图片的宽高
    :param img_path:
    :return:
    """
    image = Image.open(img_path)
    return image.size